<template>
  <div>
    <el-form :model="form" label-position="top">
      <el-form-item label="⚙ 选择审批对象">
        <div>
          <el-radio-group v-model="assignedType"  @input="radioChange" >
            <el-radio v-for="t in approvalTypes" :label="t.type" :key="t.type">{{ t.name }}</el-radio>
          </el-radio-group>
        </div>
      </el-form-item>
			<el-form-item multiple  v-if="assignedType == 'ASSIGN_USER'">
        <el-select v-model="value" filterable multiple  value-key="userId" @change="emitUser(value)" placeholder="请选择审批人">
          <el-option v-for="item in users" :key="item.userId" :label="item.nickName" :value="item"></el-option>
        </el-select>
			</el-form-item>
      <!-- <el-form-item v-if="nodeProps.assignedType == 'LEADER'">
        <el-divider>接收人的教研室主任</el-divider>
      </el-form-item> -->
      <el-form-item v-if="assignedType == 'ROLE'">
        <el-select @change="emitRole(value)" multiple  value-key="roleId" v-model="value" placeholder="请选择角色">
          <el-option v-for="item in roles" :key="item.roleId" :label="item.roleDesc" :value="item"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "ApprovalNodeConfig",
  props: {
    config: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      showOrgSelect: false,
			userList:[],
			leaderList:[],
			roleList:[],
      orgPickerSelected: [],//选择的信息
      approvalTypes: [
        {name: '指定人员', type: 'ASSIGN_USER'},
        // {name: '主任', type: 'LEADER'},
        {name: '角色', type: 'ROLE'},
      ],
      value:[],
      form:{},
      roles: [],
      users:[],
      // assignedType:"ROLE",
    }
  },
  computed: {
    nodeProps() {
      return this.$store.state.selectedNode.props
    },
    select: {
      get(){
        return this.config.assignedUser || []
      },
      set(val){
        this.config.assignedUser = val
      }
    },
    assignedType:{
      get(){
        return this.config.assignedType
      },
      set(val){
        this.config.assignedType = val
      }
    },
    selectRole:{
      get(){
        return this.config.role || []
      },
      set(val){
        this.config.role = val
      }
    },
  },
  methods: {
    emitRole(info){
      this.orgPickerSelected = this.selectRole
      let flag=this.assignedType=="ROLE"
      let obj = []
      let weight=0
      info.forEach(item=>{
        obj.push({
        id: flag?item.roleId:item.userId,
        name: flag?item.roleDesc:item.nickName,
        type: flag ? "ROLE" : "ASSIGN_USER",
      })
      //判断最小权重
      if(weight==0){
        weight=item.weight
      }else if(weight>item.weight)weight=item.weight
      })
      this.$store.commit('setWeight',weight)
      this.selected(obj)
    },

    radioChange(val){
      this.assignedType=val
      this.select=[]
      this.selectRole=[]
      this.value=[]
    },

    async initOptions(){
      let roles=await this.$get('/api/roles/list')
      let users=await this.$get('/api/approve/users')
      this.roles=roles.data
      this.users=users.data
      if(this.config.assignedUser.length>0){
        this.config.assignedUser.forEach(item=>{
          this.users.forEach(items=>{
            if(item.id==items.userId) this.value.push(items)
          })
        })
      }else if(this.config.role.length>0){
        this.assignedType="ROLE"
          this.config.role.forEach(item=>{
            this.roles.forEach(items=>{
              if(item.id==items.roleId) {
                this.value.push(items)
              }
            })
            })
          }
    },
    emitUser(info) {
      this.orgPickerSelected = this.select
      let flag=this.assignedType=="ROLE"
      let obj = []
      if(info.length>0){
        let weight=info[0].weight
        info.forEach(item=>{
          obj.push({
          id: flag?item.roleId:item.userId,
          name: flag?item.roleDesc:item.nickName,
          type: flag ? "ROLE" : "ASSIGN_USER",
        })
        //判断最大权重
        if(weight>item.weight)weight=item.weight
        })
        this.$store.commit('setWeight',weight)
      }else{
        this.$store.commit('setWeight',0)
      }
      this.selected(obj)
    },
    selected(select) {
      this.orgPickerSelected.length = 0
      if(select.length>0){
        select.forEach(val => this.orgPickerSelected.push(val))
      }this.orgPickerSelected=[]
    },
  },
  created() {
    let data = this.select || this.selectRole
    this.value = data[0].name
  },
  mounted(){
    this.initOptions()
  }
}
</script>

<style lang="scss" scoped>
</style>
